package log

import (
	"context"
	"flag"
	"gateway/config/logsystem"
	"github.com/yandaren/go-slog/slog"
	"google.golang.org/grpc"
	"net/http"
	"strconv"
	"time"
)

var (
	LogSystemAddr = flag.String("LogSystemAddr", "logsystem:16015", "the address to connect to")
)
var conn, err = grpc.Dial(*LogSystemAddr, grpc.WithInsecure())

//var c = leaderboard.NewProdScoreBoardServiceClient(conn)
var c = logsystem.NewLogSystemClient(conn)

func WriteLog(ctx context.Context, servername string, funcName string, userId int, lever string, describe string) (int, error) {
	response, err := c.WriteLogInfo(ctx, &logsystem.WriteLogFormRequest{
		ServerName: servername,
		Time:       time.Now().Format("2006-01-02 15:04:05"),
		Function:   funcName,
		UserId:     strconv.Itoa(userId),
		Lever:      lever,
		Describe:   describe,
	})
	if err != nil {
		return http.StatusBadRequest, err
	}

	stderrLogger := slog.NewStderrLoggerSt("stderr_logger")
	if lever == "Debug" {
		stderrLogger.Debug("UserId:" + strconv.Itoa(userId) + "\tFunctionName:" + funcName + "\tServerName:" + servername + "\tDescribe" + describe)
	}
	if lever == "Warning" {
		stderrLogger.Warn("UserId:" + strconv.Itoa(userId) + "\tFunctionName:" + funcName + "\tServerName:" + servername + "\tDescribe" + describe)
	}
	if lever == "Error" {
		stderrLogger.Error("UserId:" + strconv.Itoa(userId) + "\tFunctionName:" + funcName + "\tServerName:" + servername + "\tDescribe" + describe)
	}

	return int(response.Status), err
}
